package com.leyou.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 操作日志处理
 */
@Aspect
@Component
public class LogAcpect {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogAcpect.class);

    /**
     * log切入点

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void logPoint(){

    }*/

    /**
     * 方法之前执行打印日志信息

    @Before("logPoint()")
    public void doBefore(JoinPoint joinPoint){
        // 获取类名
        String className = joinPoint.getTarget().getClass().getName();
        // 获取方法名
        String methodName = joinPoint.getSignature().getName();
        // 获取请求参数
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        Map<String, String[]> parameterMap = request.getParameterMap();
        LOGGER.info("requestUrl：{}",request.getRequestURI());
        LOGGER.info("requestClassName:{},requestMethodName:{}",className,methodName);
        LOGGER.info("requestParam:{}",parameterMap.toString());
    }*/
}
